home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jsdk / PhoneSer.jav < prev    next >
Encoding:
Text File  |  1997-08-17  |  3.0 KB  |  111 lines

  1. /*
  2.  * @(#)PhoneServlet.java    1.37 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21. import java.lang.*;
  22. import java.io.*;
  23. import java.net.*;
  24. import java.util.*;
  25.  
  26. import javax.servlet.*;
  27. import javax.servlet.http.*;
  28.  
  29. /**
  30.  * @author  Pavani Diwanji
  31.  */
  32. public
  33. class PhoneServlet extends HttpServlet { 
  34.     private Hashtable phones;
  35.  
  36.     public void init(ServletConfig conf) throws ServletException {
  37.         int next;
  38.         String name, number;
  39.  
  40.     super.init(conf);
  41.         log("PhoneServlet.initialize: enter");
  42.  
  43.         String fileName = getInitParameter("phonelist");
  44.     if (fileName == null) {
  45.         return;
  46.     }
  47.  
  48.         log("PhoneServlet.initialize: filename = " + fileName);
  49.  
  50.     try {
  51.         FileInputStream fin = new FileInputStream(fileName);
  52.         phones = new Hashtable();
  53.         StreamTokenizer st = new StreamTokenizer(fin);
  54.         st.eolIsSignificant(true);
  55.         st.slashSlashComments(true);
  56.  
  57.         while ((next = st.nextToken()) != StreamTokenizer.TT_EOF) {
  58.         switch (next) {
  59.           case StreamTokenizer.TT_WORD:
  60.             name = st.sval;
  61.             next = st.nextToken();
  62.             if (next != StreamTokenizer.TT_WORD) {
  63.             throw new IOException("No phone for name: " + name);
  64.             }
  65.             number = st.sval;
  66.             break;
  67.           default:
  68.             continue;
  69.         }
  70.         phones.put(name, number);        
  71.         }
  72.     } catch (IOException e) {
  73.         phones = null;
  74.     }
  75.     }
  76.  
  77.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  78.     throws ServletException, IOException
  79.     {
  80.     /*
  81.      * If the query is for a specific person then return their
  82.      * extension, else dump the whole extensions list
  83.      */
  84.     res.setContentType("text/html");
  85.     ServletOutputStream out = res.getOutputStream();
  86.     String name = req.getParameter("name");
  87.  
  88.     String title = "Phones";
  89.         out.println
  90.         ("<HEAD><TITLE> Phone Servlet Output </TITLE></HEAD><BODY>");
  91.  
  92.     out.println("<h1> PhoneServlet output </h1>");
  93.     out.println("<p>");
  94.    
  95.     if (phones == null) {
  96.         out.println("No Phone List!");
  97.     } else if (name != null) {
  98.         String phone = (String) phones.get(name);
  99.         if (phone == null) phone = "Not listed";
  100.         out.println(name + ": " + phone);
  101.     } else
  102.         out.println(phones.toString());
  103.  
  104.         out.println("</BODY>");
  105.     }
  106.  
  107.     public String getServletInfo() {
  108.     return "This servlet is useful for looking up phone extensions.";
  109.     }
  110. }
  111.